{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "uuid": "bef8ebe5-b72c-4618-89fd-8b7c1316121d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1797, 64)\n",
      "[[ 0.  0.  5. 13.  9.  1.  0.  0.]\n",
      " [ 0.  0. 13. 15. 10. 15.  5.  0.]\n",
      " [ 0.  3. 15.  2.  0. 11.  8.  0.]\n",
      " [ 0.  4. 12.  0.  0.  8.  8.  0.]\n",
      " [ 0.  5.  8.  0.  0.  9.  8.  0.]\n",
      " [ 0.  4. 11.  0.  1. 12.  7.  0.]\n",
      " [ 0.  2. 14.  5. 10. 12.  0.  0.]\n",
      " [ 0.  0.  6. 13. 10.  0.  0.  0.]]\n",
      "0\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPUAAAEICAYAAACHyrIWAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy86wFpkAAAACXBIWXMAAAsTAAALEwEAmpwYAAAPgklEQVR4nO3dfYwc9X3H8fcnBpTwFLuEgnPmISaRVZo0gF0a6gIJ0BYaAqQCByoeG8mOIhCoqcBErVLUh1DUoqRqmsgxEBoTICbQJohCbQUMTinBB24LHCTgQjibBye2ZQwtBvPtHzPXrI+Dm92bnd395vOSTre7M7e/7+zt536zczO/nyICM8vjHb0uwMzq5VCbJeNQmyXjUJsl41CbJeNQmyXjUPcpSX8maVkNz3O0pCfqqKlb2qlxELan1xzqiiQ9LemEcY+dL2l1r2qqIiLui4g5Y/fHb4ekgyWFpF260X75x+k1SS+VXz+S9PeSZr5VjW9nsu0xhzq1bgW1AzdHxF7ALwGfBPYHhluDbfVxqGskabGkp8oe6TFJn2xZdr6k1ZL+RtJmSf8t6aSW5e+TtKr82RXAe1qWXS/pc+XtobJn/Wx5//2SNqnwUUmjki6T9Dxw3dhj5brfB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bWlH3rqI4EnI2JdRGwHbgJO7XFNUxYRz0XEQ+Xtl4ARYKi3VdVD0izg48DSXtdSJ0l7A8cA1wBExPZBCzT0R6iHgGdb7o+S5M0/RtLBwOHAAz0upS5fAi4F3uhxHXWbDWwEris/WiyVtEevi2pXP4RaEzyW5v9skvYEvgNcEhFbe13PVEk6GXgxIoZ7XUsX7AIcAXw1Ig4HXgYG7hhPP4R6FDig5f4sYEOPaqmVpF0pAn1DRGQZXnk+cIqkpyk+Kh0naVlvS6rNKDAaEWN7VLdQhHyg9EOoHwQ+IOl95YGJM4Hv9rimKZMkis9mIxFxda/rqUtEXB4RsyLiYIrf1fcj4uwel1WLiHgeeFbSnPKh44GBO7DZ7gR5tYuI1yVdCNwFTAOujYhHe1xWHeYD5wD/JWlt+djnI+KO3pVkFVwE3FB2MOuAC3pcT9t6/i8tM6tXP+x+m1mNHGqzZBxqs2QcarNkHGqzZBxqs2QcarNk/g+zE0nbbBxu1wAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "# 使用LR进行MNIST手写数字分类\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import preprocessing\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.datasets import load_digits\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 加载数据\n",
    "digits = load_digits()\n",
    "data = digits.data\n",
    "# 数据探索\n",
    "print(data.shape)\n",
    "# 查看第一幅图像\n",
    "print(digits.images[0])\n",
    "# 第一幅图像代表的数字含义\n",
    "print(digits.target[0])\n",
    "# 将第一幅图像显示出来\n",
    "plt.gray()\n",
    "plt.title('Handwritten Digits')\n",
    "plt.imshow(digits.images[0])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "uuid": "9c3af58a-2fca-460b-abe5-9228b40e9fa1"
   },
   "outputs": [],
   "source": [
    "# 分割数据，将25%的数据作为测试集，其余作为训练集\n",
    "train_x, test_x, train_y, test_y = train_test_split(data, digits.target, test_size=0.25, random_state=33)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建cart分类器\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "cart_model = DecisionTreeClassifier(criterion='gini', splitter='best', max_depth=21, min_samples_split=4, \n",
    "                       min_samples_leaf=2,  \n",
    "                       random_state=2030)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "uuid": "f17ed425-f5c4-4f9f-88c0-12d3dc01b319"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'max_depth': 18, 'min_samples_leaf': 2, 'min_samples_split': 2}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 网格搜索\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "# 预设各参数的不同选项值\n",
    "max_depth = [18,19,20,21,22]\n",
    "min_samples_split = [2,4,6,8]\n",
    "min_samples_leaf = [2,4,8,10,12]\n",
    "parameters = {'max_depth':max_depth, 'min_samples_split':min_samples_split, 'min_samples_leaf':min_samples_leaf}\n",
    "# 网格搜索法，测试不同的参数值\n",
    "grid_dtcateg = GridSearchCV(estimator = cart_model, param_grid = parameters, cv=10)\n",
    "# 模型拟合\n",
    "grid_dtcateg.fit(train_x, train_y)\n",
    "# 返回最佳组合的参数值\n",
    "grid_dtcateg.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "best_model=grid_dtcateg.best_estimator_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(max_depth=18, min_samples_leaf=2, random_state=2030)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CART准确率: 0.8533\n"
     ]
    }
   ],
   "source": [
    "best_model.fit(train_x,train_y)\n",
    "predict_y=best_model.predict(test_x)\n",
    "print('CART准确率: %0.4lf' % accuracy_score(predict_y, test_y))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
